package com.example.administrator.futurehope;

import java.util.Map;

import com.download.ThreadDownloadActivity;
import com.fhuc.utils.AppInfo;
import com.fhuc.utils.CustomProgressDialog;
import com.fhuc.utils.MyDialog;
import com.fhuc.utils.Sharepf;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public abstract class BaseActivity extends AppCompatActivity {
	public Sharepf sp;
	public ImageView image_back, image_ico, set;
	public Button btn, btn_title_login;
	public RelativeLayout back;
	public TextView tx_login_title;
	public CustomProgressDialog progressDialog;
	public FhucAppliacation app;
	public updateAppReceiver Rreceiver;
	private AppInfo af;
	private boolean QRDialog = false;
	private MyDialog dialog;
	public String loginname = "", loginpassw = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		app = (FhucAppliacation) getApplication();
		sp = new Sharepf(BaseActivity.this);
		Rbrod();
		Islogin();
	}

	public void Islogin() {
		// if (app.isIsinit()) {
		// SharedPreferences userInfo =
		// getApplicationContext().getSharedPreferences("rememberPassword", 0);
		// String name = userInfo.getString("name", "");//
		// String passw = userInfo.getString("password", "");//
		// if (!name.equals("") && !passw.equals("")) {
		// loginname = name;
		// loginpassw = passw;
		// }
		// else
		// {
		// Intent intentlongin = new Intent(getApplicationContext(),
		// LoginActivity.class);
		// startActivity(intentlongin);
		// }
		// }

		if (sp.readRememberPassword()) {
			Map<String, String> map = sp.readUser();
			String name = map.get("name");
			String psw = map.get("password");
			if (!psw.equals("")) {
				loginpassw = psw;
				loginname=name;
			}
		}
	}

	@Override
	protected void onStart() {
		QRDialog = true;
		super.onStart();
	}

	@Override
	protected void onStop() {
		QRDialog = false;
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		if (Rreceiver != null)

		{
			this.unregisterReceiver(Rreceiver);
		}
		super.onDestroy();
	}

	public void Rbrod() {
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction("com.updateApp");
		Rreceiver = new updateAppReceiver();
		this.registerReceiver(Rreceiver, intentFilter);
	}

	class updateAppReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			af = (AppInfo) intent.getSerializableExtra("AppInfo");

			if (QRDialog) {
				downlodapp();
			}
		}

	}

	public void downlodapp() {
		if (dialog == null) {
//			dialog = new MyDialog(this, R.style.MyDialog);
			dialog = new MyDialog(this);
		}
		if (!isFinishing()) {
			if (!dialog.isShowing()) {
				dialog.show();
				app.setIsshow(true);
				dialog.setMsg("检测到新版本", "版本号：" + af.getVersionCode() + "\n发布时间：" + af.getVersionDate() + "\n是否下载更新？");
				dialog.setOnPositiveButton("确定", new OnClickListener() {
					@Override
					public void onClick(View v) {
						// Intent it = new Intent(BaseActivity.this,
						// NotificationUpdateActivity.class);
						// it.putExtra("URL", APPURL);
						// it.putExtra("fileName", APPfilename);
						// startActivity(it);
						Intent intent = new Intent(BaseActivity.this, ThreadDownloadActivity.class);
						intent.putExtra("URL", af.getURL());
						intent.putExtra("fileName", "FHUC.APK");
						startActivity(intent);
						dialog.dismiss();
					}
				});
				dialog.setOnNegativeButton("取消", new OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
				});

			}
		}

	}

	// public void Islogin() {
	// if (app.getUser() == null) {
	// Intent intentlongin = new Intent(getApplicationContext(),
	// LoginActivity.class);
	// startActivity(intentlongin);
	// }
	// }

	public void showToast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_LONG).show();
	}

	public boolean isnull(String str) {
		if (str != null && !str.equals("")) {
			return false;
		}
		return true;
	}

	public void startProgressDialog(String str) {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(this);
		}
		if (progressDialog!=null) {
			progressDialog.setMessage(str);
			progressDialog.setCanceledOnTouchOutside(false);
			progressDialog.show();
			
		}
	}

	public void stopProgressDialog() {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	public abstract void findView();

	public abstract void init();

	public abstract void SetListner();

}
